package avicit.cbb.businessclass.fondsmanage.action;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import avicit.cbb.businessclass.fondsmanage.service.api.CbbFondsDbServiceApi;
import avicit.platform6.commons.utils.JsonHelper;
import avicit.platform6.core.context.ThreadContextHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import avicit.cbb.businessclass.fondsmanage.domain.CbbFondsDb;
import avicit.cbb.businessclass.fondsmanage.domain.CbbFondsDbV;
import avicit.cbb.businessclass.fondsmanage.domain.CbbFondsServer;
import avicit.cbb.businessclass.fondsmanage.domain.CbbFondsServerV;
import avicit.cbb.businessclass.fondsmanage.service.CbbFondsDbService;
import avicit.im.common.CommonTreeNode;
import avicit.platform6.api.session.SessionHelper;
import avicit.platform6.api.syslookup.SysLookupAPI;
import avicit.platform6.api.syslookup.dto.SysLookupSimpleVo;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.commons.utils.JsonUtil;
//import avicit.platform6.commons.utils.JsonUtils;
import avicit.platform6.core.dao.Paging;
import avicit.platform6.core.jdbc.Page;
import avicit.platform6.core.properties.PlatformConstant;
import avicit.platform6.core.properties.PlatformConstant.OpResult;

/**
 * <p>
 * 金航数码科技有限责任公司
 * </p>
 * <p>
 * 作者：张坚
 * </p>
 * <p>
 * 邮箱：18629436468@163.com
 * </p>
 * <p>
 * 创建时间： 2014-9-22 上午10:18:27
 * </p>
 * <p>
 * 类说明：全宗管理控制器类
 * </p>
 * <p>
 * 修改记录：
 * </p>
 */
@Controller
@RequestMapping("/cbbFondsDbController")
@SuppressWarnings("deprecation")
public class CbbFondsDbController {

	@Resource
	private CbbFondsDbServiceApi cbbFondsDbService;
	@Resource
	private CommSysInfoIntfsService commSysInfoIntfsService;

	// 加载通用代码对象
	@Resource
	private SysLookupAPI upLoader;

	/**
	 * 加载组织部门树
	 * 
	 * @author 张坚
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *             ModelAndView
	 * @throws
	 */
	@RequestMapping("/getChildsNode")
	@ResponseBody
	public Map<String,Object> getChildsNode(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String parentId = "-1";
		String deptId = ThreadContextHelper.getDeptId();
		String orgId = ThreadContextHelper.getOrgId();
		List<CommonTreeNode> nodes = new ArrayList<CommonTreeNode>();
		String id = parentId;
		try {
			// 默认为根节点
			if (StringUtils.isEmpty(id)) {
				id = "-1";
			}
			nodes = cbbFondsDbService.getChildsNode(id, deptId, orgId);
			map.put("result", OpResult.success.ordinal());
		} catch (Exception e) {
			map.put("result", OpResult.failure.ordinal());
			map.put("msg", "后台处理异常，" + e.getMessage());
		}
		map.put("data", nodes);
		return map;
	}

	/**
	 * 查询全宗信息
	 * 
	 * @author 张坚
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *             ModelAndView
	 * @throws
	 */
	@RequestMapping("/loadCbbFondsDb")
	@ResponseBody
	public Map<String,Object> loadCbbFondsDb(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String nodeId = request.getParameter("nodeId"); // 这里的nodeId即是全宗ID
		String pageNo = request.getParameter("page");// 取得当前页码
		String pageSize = request.getParameter("rows");// 取得一页显示的数量
		Paging<CbbFondsDbV> page = new Paging<CbbFondsDbV>();
		page.setPageSize(ComUtil.getPageSize(pageSize));
		page.setPageNo(ComUtil.getPageNo(pageNo));
		String deptId = request.getParameter("deptId");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";// 系统ID
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("deptId", deptId);
		parameter.put("sysId", sysId);
		parameter.put("orgId", nodeId);
		Page<Map<String, Object>> result = cbbFondsDbService.loadCbbFondsDb(page, parameter);
		map.put("total", result.getEntityCount()); // total键
		map.put("rows", result.getEntities()); // rows键 存放每页记录 list
		return map;
	}

	/**
	 * 查询全文服务器设置信息
	 * 
	 * @author 张坚
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 *             ModelAndView
	 * @throws
	 */
	@RequestMapping("/loadCbbFondsServer")
	@ResponseBody
	public Map<String,Object> loadCbbFondsServer(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Map<String,Object> map = new HashMap<>();
		String pageNo = request.getParameter("page");// 取得当前页码
		String pageSize = request.getParameter("rows");// 取得一页显示的数量
		Paging<CbbFondsServerV> page = new Paging<CbbFondsServerV>();
		page.setPageSize(ComUtil.getPageSize(pageSize));
		page.setPageNo(ComUtil.getPageNo(pageNo));
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";// 系统ID
		String cbbFondsDbId = request.getParameter("cbbFondsDbId");
		String fullsvrType = request.getParameter("fullsvrType");
		String orgId = ThreadContextHelper.getOrgId();
		Map<String, Object> parameter = new HashMap<String, Object>();
		parameter.put("sysId", sysId);
		parameter.put("orgId", orgId);
		parameter.put("cbbFondsDbId", cbbFondsDbId);
		parameter.put("fullsvrType", fullsvrType);
		Page<Map<String, Object>> result = cbbFondsDbService.loadCbbFondsServer(page, parameter);
		map.put("total", result.getEntityCount()); // total键
		map.put("rows", result.getEntities()); // rows键 存放每页记录 list
		return map;
	}

	/**
	 * 删除全宗信息
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping({"/deleteCbbFondsDb"})
	@ResponseBody
	public Map<String,Object> deleteCbbFondsDb(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<>();
		try {
			String ids = request.getParameter("ids");
			if ((ids != null) && (!"".equals(ids))) {
				this.cbbFondsDbService.deleteCbbFondsDb(ids);
				this.cbbFondsDbService.deleteCbbFondsServerByDbId(ids);
				map.put("flag", OpResult.success);
			}
		} catch (Exception e) {
			map.put("flag", OpResult.failure);
		}
		return map;
	}

	/**
	 * 删除全文服务器设置信息
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping({"/deleteCbbFondsServer"})
	@ResponseBody
	public Map<String,Object> deleteCbbFondsServer(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<>();
		try {
			String ids = request.getParameter("ids");
			if ((ids != null) && (!"".equals(ids))) {
				this.cbbFondsDbService.deleteCbbFondsServer(ids);
				map.put("flag", OpResult.success);
			}
		} catch (Exception e) {
			map.put("flag", OpResult.failure);
		}
		return map;
	}

	/**
	 * 全宗添加
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping("/toDbAddJsp")
	public ModelAndView toDbAddJsp(HttpServletRequest request) {
		String orgId = request.getParameter("orgId"); // 组织ID
		String sysId = request.getParameter("sysId"); // 部门编号
		Map<String, Object> orgInfoList = this.cbbFondsDbService.getSysOrgInfo(orgId);
		String odCode = "";
		String odName = "";
		if (orgInfoList.size() > 0) {
			odCode = (String) orgInfoList.get("OD_CODE");
			odName = (String) orgInfoList.get("OD_NAME");
		}
		ModelAndView mav = new ModelAndView();
		mav.addObject("id", ComUtil.getId());
		mav.addObject("ORG_ID", orgId);
		mav.addObject("OD_CODE", odCode);
		mav.addObject("OD_NAME", odName);
		mav.addObject("sysId", sysId);
		mav.addObject("dialogId", request.getParameter("dialogId"));
		mav.setViewName("avicit/cbb/businessclass/fondsmanage/CbbFondsDbAdd");
		return mav;
	}

	/**
	 * 全宗编辑
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping("/toDbEditJsp")
	public ModelAndView toDbEditJsp(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String id = String.valueOf(request.getParameter("id"));
		List<Map<String, Object>> list = this.cbbFondsDbService.getCbbFondsDbV(id);
		Map<String, Object> map = new HashMap<String, Object>();
		if (list.size() > 0) {
			map = list.get(0);
		}
		mav.addObject("cbbFondsDbV", map);
		mav.addObject("dialogId", request.getParameter("dialogId"));
		mav.setViewName("avicit/cbb/businessclass/fondsmanage/CbbFondsDbEdit");
		return mav;
	}

	/**
	 * 保存全宗管理记录
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping("saveCbbFondsDb")
	@ResponseBody
	public Map<String,Object> saveCbbFondsDb(HttpServletRequest request) throws Exception {
		Map<String,Object> map = new HashMap<>();

		String type = request.getParameter("type");
		String sysId = request.getParameter("sysId");
		sysId = "LANTOP";// 系统IDJsonHelper.getInstance().readValue
		CbbFondsDb cbbFondsDb = (CbbFondsDb) JsonHelper.getInstance().readValue(request.getParameter("datas"), CbbFondsDb.class);
		String orgId = cbbFondsDb.getOrgId();
		String poolName = orgId+"_"+sysId;
		if (type.equals("save")) {
			// 保存
			try {
				cbbFondsDb.setDeptId(ThreadContextHelper.getDeptId());
				cbbFondsDb.setFondsDeptId(cbbFondsDb.getOrgId());
				cbbFondsDb.setSysId(sysId);
				this.cbbFondsDbService.insertCbbFondsDb(cbbFondsDb);
				//连接池管理类
				DBConnectionCustomManager dBConnectionManager = DBConnectionCustomManager.getInstance(poolName);
				dBConnectionManager.saveConnectionPool(poolName);

				map.put("flag", OpResult.success.ordinal());
			} catch (Exception e) {
				map.put("flag", OpResult.failure.ordinal());
				e.printStackTrace();
			}

		} else {
			// 编辑
			try {
				this.cbbFondsDbService.updateCbbFondsDb(cbbFondsDb);
				//连接池管理类
				DBConnectionCustomManager dBConnectionManager = DBConnectionCustomManager.getInstance(poolName);
				dBConnectionManager.saveConnectionPool(poolName);

				map.put("flag", OpResult.success.ordinal());
			} catch (Exception e) {
				map.put("flag", OpResult.failure.ordinal());
				e.printStackTrace();
			}
		}
		return map;
	}

	/**
	 * 跳转到全文服务器信息设置添加页面
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping("/toServerAddJsp")
	public ModelAndView toServerAddJsp(HttpServletRequest request) {
		String cbbFondsDbId = request.getParameter("cbbFondsDbId"); // 全宗ID
		String fondsDeptId = request.getParameter("fondsDeptId");
		String sysId = request.getParameter("sysId");
		Map<String, Object> orgInfoMap = this.cbbFondsDbService.getSysOrgInfo(fondsDeptId);
		ModelAndView mav = new ModelAndView();
		mav.addObject("id", ComUtil.getId());
		mav.addObject("sysId", sysId);
		mav.addObject("cbbFondsDbId", cbbFondsDbId);
		mav.addObject("OD_CODE", orgInfoMap.get("OD_CODE"));
		mav.addObject("OD_NAME", orgInfoMap.get("OD_NAME"));
		Collection<SysLookupSimpleVo> cbbFullsvrTypeList = upLoader.getLookUpListByType("CBB_FULLSVR_TYPE"); // 全文服务器类型
		mav.addObject("cbbFullsvrTypeList", cbbFullsvrTypeList);
		mav.addObject("dialogId", request.getParameter("dialogId"));
		mav.setViewName("avicit/cbb/businessclass/fondsmanage/CbbFondsServiceAdd");
		return mav;
	}

	/**
	 * 跳转到全文服务器信息设置编辑页面
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping("/toServerEditJsp")
	public ModelAndView toServerEditJsp(HttpServletRequest request) {
		ModelAndView mav = new ModelAndView();
		String id = request.getParameter("id"); // 全文记录ID
		List<Map<String, Object>> list = this.cbbFondsDbService.getCbbFondsServerV(id);
		Map<String, Object> map = new HashMap<String, Object>();
		if (list.size() > 0) {
			map = list.get(0);
		}
		mav.addObject("cbbFondsServerV", map);
		Collection<SysLookupSimpleVo> cbbFullsvrTypeList = upLoader.getLookUpListByType("CBB_FULLSVR_TYPE"); // 全文服务器类型
		mav.addObject("cbbFullsvrTypeList", cbbFullsvrTypeList);
		mav.addObject("dialogId", request.getParameter("dialogId"));
		mav.setViewName("avicit/cbb/businessclass/fondsmanage/CbbFondsServiceEdit");
		return mav;
	}

	/**
	 * 保存添加/修改的全文服务器信息设置记录
	 * 
	 * @author 张坚
	 * @param request
	 * @return ModelAndView
	 * @throws
	 */
	@RequestMapping("saveCbbFondsServer")
	@ResponseBody
	public Map<String,Object> saveCbbFondsServer(HttpServletRequest request) {
		Map<String,Object> map = new HashMap<>();

		String type = request.getParameter("type");
		String sysId = request.getParameter("sysId");
		CbbFondsServer cbbFondsServer = (CbbFondsServer) JsonHelper.getInstance().readValue(request.getParameter("datas"), CbbFondsServer.class);
		if (type.equals("save")) {
			// 保存
			try {
				cbbFondsServer.setOrgId(ThreadContextHelper.getOrgId());
				cbbFondsServer.setDeptId(ThreadContextHelper.getDeptId());
				cbbFondsServer.setSysId(sysId);
				this.cbbFondsDbService.insertCbbFondsServer(cbbFondsServer);
				map.put("flag", OpResult.success.ordinal());
			} catch (Exception e) {
				map.put("flag", OpResult.failure.ordinal());
				e.printStackTrace();
			}

		} else {
			// 编辑
			try {
				this.cbbFondsDbService.updateCbbFondsServer(cbbFondsServer);
				map.put("flag", OpResult.success.ordinal());
			} catch (Exception e) {
				map.put("flag", OpResult.failure.ordinal());
				e.printStackTrace();
			}
		}
		return map;
	}

	/**
	 * 保存唯一验证
	 * @author 张坚 
	 * @param request
	 * @return ModelAndView
	 * @throws Exception 
	 */
	@RequestMapping("/saveUniqueValidate")
	@ResponseBody
	public Map<String,Object> saveUniqueValidate(HttpServletRequest request) throws Exception {
		Map<String,Object> map = new HashMap<>();

		String fullsvrType = request.getParameter("fullsvrType");
		String svrAddress = request.getParameter("svrAddress");
		String svrPublishPath = request.getParameter("svrPublishPath");
		String ftpUser = request.getParameter("ftpUser");

		boolean bool = this.cbbFondsDbService.saveUniqueValidate(fullsvrType, svrAddress, svrPublishPath, ftpUser);
		if (bool) {
			map.put("validate", "1");
		} else {
			map.put("validate", "0");
		}
		return map;
	}

	/**
	 * 编辑唯一验证
	 * @author 张坚 
	 * @param request
	 * @return ModelAndView
	 * @throws Exception 
	 */
	@RequestMapping("/editUniqueValidate")
	@ResponseBody
	public Map<String,Object> editUniqueValidate(HttpServletRequest request) throws Exception {
		Map<String,Object> map = new HashMap<>();

		String id = request.getParameter("id");
		String fullsvrType = request.getParameter("fullsvrType");
		String svrAddress = request.getParameter("svrAddress");
		String svrPublishPath = request.getParameter("svrPublishPath");
		String ftpUser = request.getParameter("ftpUser");

		boolean bool = this.cbbFondsDbService.editUniqueValidate(id, fullsvrType, svrAddress, svrPublishPath, ftpUser);
		if (bool) {
			map.put("validate", "1");
		} else {
			map.put("validate", "0");
		}
		return map;
	}

}
